<?php


function _requestGet($url, $ssl = true) {
    // curl实例化
    $curl = curl_init();
    //设置curl选项
    //URL
    curl_setopt($curl, CURLOPT_URL, $url);
    //user_agent，请求代理信息
    $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'Mozilla/5.0 (Windows NT 5.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0 FirePHP/0.7.4';
    curl_setopt($curl, CURLOPT_USERAGENT, $user_agent);
    //referer头，请求来源
    curl_setopt($curl, CURLOPT_AUTOREFERER, true);
    //SSL相关
    if ($ssl) {
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //禁用后cURL将终止从服务端进行验证
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); //检查服务器SSL证书中是否存在一个公用名(common name)。
    }
    //curl_setopt($curl, CURLOPT_HTTPPROXYTUNNEL, 0); 
   // curl_setopt($curl, CURLOPT_PROXY, '192.187.111.11:443');
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);  //连接超时
    curl_setopt($curl, CURLOPT_TIMEOUT, 20);          //响应超时  单位 秒，也可以使用
    curl_setopt($curl, CURLOPT_HEADER, false); //是否处理响应头
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); //curl_exec()是否输出响应结果
    // 发出请求
    $response = curl_exec($curl);
    // if (false === $response) {
    //     echo '<br>', curl_error($curl), '<br>';
    //     return curl_error($curl);
    // }
    return $response;
}


//报告运行时错误
error_reporting(E_ERROR | E_WARNING | E_PARSE);
set_time_limit(0);

include('simplehtmldom_1_9_1/simple_html_dom.php');

$text = file_get_contents("bb.txt");
$allget = file_get_contents("allget.txt");
$arr = explode(",",$text);
$allget_arr = explode(",",$allget);

$i = 1;
foreach($arr as $num){
    if(in_array($num,$allget_arr)){
        $i++;
        continue;
    } 

    $url = 'https://www.soshuw.com/ZheTian/'.$num.'.html';

    do{
        echo "{$num},第{$i}章 开始请求  \n";
        $res=_requestGet($url);
    }while(!$res);


    $html = str_get_html($res);
    $title = $html->find('.read_title h1')[0]->innertext;
    $content = $html->find('.content')[0]->innertext;

    $content = str_replace("<br />","\n", $content); 
    $content = str_replace("&nbsp;","", $content); 

    $content = trim($content);
    file_put_contents("content.txt",$title."\n".$content."\n\n",FILE_APPEND);

    file_put_contents("allget.txt",$num.",",FILE_APPEND);
    
    echo $num.",第{$i}章 加载完毕\n";

    $i++;
    usleep(2000);
}



die;
